<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $news;

  function PageController() {
	parent::__construct();

	$this->title="News";
	$this->section="news";

	$action = $this->getParameter("action");
	$this->news = new NewsItem();

	switch (strtoupper($action))
	{
		case "SAVE" :
			  $this->parseForm();
			  if (!$this->validate()) return;

			  if (!NewsLogic::save($this->news))
			  {
			  	 $this->addError("Could not save news item.");
			  	 break;
			  }

			  if (strlen($this->tags) > 0)
			  {
			  	$tagsArray = split("[ ,]+", $this->tags);
			  	 if (!NewsLogic::saveTags($this->news->id, $tagsArray))
			  	 {
			  	 	 $this->addError("Couldn't tag news item");
			  	 }
			  }

			  $this->addMessage("Saved");
			  break;

		case "LOAD" :
			  $id = $this->getParameter("id");
	  		  $this->news = NewsLogic::load($id);
	  		  $this->tags = implode(" ",  NewsLogic::loadTags($id));
	  		  $this->addMessage("Loaded $id");
	  		  break;

	}

  }

  function parseForm() {
	$this->news->readForm();

	$this->tags = $this->getParameter("tags");
  }

  function validate() {
  	if ($this->news->item == "")
  		$this->addError("An entry is required");
  	if ($this->news->visibility == "")
  		$this->addError("Visibility is required");
	return (sizeof($this->errs) == 0);
  }

}



?>
